package gov.fnal.mcginnis.threetone;


import gov.fnal.mcginnis.threetone.ThreeToneGui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * @author mcginnis
 *
 */
class ThreeToneGuiListeners implements ActionListener, ChangeListener, MouseListener, ComponentListener  
{
	ThreeToneGui threeToneGui = null;
	String listenString = null;
	ThreeToneGuiListeners(ThreeToneGui threeToneGui, String listenString)
	{
		this.threeToneGui = threeToneGui;
		this.listenString = listenString;
	}
	
	public void actionPerformed(ActionEvent arg0) 
	{
//		System.out.println(listenString);
		if (listenString.equals("File.Open"))
			threeToneGui.getImageFile();
		if (listenString.equals("File.Save"))
			threeToneGui.saveFile();
		if (listenString.equals("Help.Help"))
		{
			threeToneGui.mainTabbedPane.setSelectedIndex(2);
		}
		if (listenString.equals("Help.About"))
			threeToneGui.messageDialog("Tommy Three Tone " + threeToneGui.version + "\nGoofOff Software Inc.\n" + "Copyright 2008\n" + "Last Updated " + threeToneGui.versionDate);
		if (listenString.equals("File.Exit"))
			System.exit(0);
		if ("Grey Linear".equals(listenString))
		{
			threeToneGui.autoLevelReset = true;
			threeToneGui.rGBplotter.setScaleType("linear");
			threeToneGui.resizeMainPanels();
			threeToneGui.plotImage();
		}
		if ("Grey Log".equals(listenString))
		{
			threeToneGui.autoLevelReset = true;
			threeToneGui.rGBplotter.setScaleType("log");
			threeToneGui.resizeMainPanels();
			threeToneGui.plotImage();
		}
		if ("Grey Hist Eq".equals(listenString))
		{
			threeToneGui.autoLevelReset = true;
			threeToneGui.rGBplotter.setScaleType("hist");
			threeToneGui.resizeMainPanels();
			threeToneGui.plotImage();
		}
		if ("Grey Invert Image".equals(listenString))
		{
			threeToneGui.rGBplotter.setInvertImage(!threeToneGui.rGBplotter.getInvertImage());
			threeToneGui.resizeMainPanels();
			threeToneGui.plotImage();
		}
		if ("Grey Auto Scale".equals(listenString))
		{
			threeToneGui.autoLevelReset = true;
			threeToneGui.rGBplotter.setAutoScale(!threeToneGui.rGBplotter.getAutoScale());
			threeToneGui.resizeMainPanels();
			threeToneGui.plotImage();
		}
		if ("Grey Color Spectrum".equals(listenString))
		{
			threeToneGui.rGBplotter.setColorSpectrum(!threeToneGui.rGBplotter.getColorSpectrum());
			threeToneGui.resizeMainPanels();
			threeToneGui.plotImage();
		}
		if ("Number of Levels".equals(listenString))
		{
			threeToneGui.autoLevelReset = true;
			threeToneGui.inumLevels = threeToneGui.noLevelComboBox.getSelectedIndex() + 1;
			threeToneGui.resizeMainPanels();
			threeToneGui.plotImage();
		}

		for (int ii = 0; ii < threeToneGui.imaxNoOfPossibleColors; ++ii)
		{
			if (listenString.equals(threeToneGui.colorButtonString[ii]))
				threeToneGui.colorButtonSelected(ii);
		}
		
		if (listenString.equals("Clear"))
			threeToneGui.colorButtonSelected(threeToneGui.imaxNoOfPossibleColors - 1);
		if (listenString.equals("Color Accept 0"))
			threeToneGui.colorAccepted(0);
		if (listenString.equals("Color Accept 1"))
			threeToneGui.colorAccepted(1);
		if (listenString.equals("Color Accept 2"))
			threeToneGui.colorAccepted(2);
		if (listenString.equals("Color Accept 3"))
			threeToneGui.colorAccepted(3);
		if (listenString.equals("gridSetup"))
		{
			threeToneGui.ngrid = threeToneGui.gridSetupComboBox.getSelectedIndex() + 1;
			threeToneGui.plotImage();
		}
		if (listenString.equals("aspectSetup"))
			threeToneGui.readLayoutSetup();
		if (listenString.equals("aspectTextField"))
		{
			threeToneGui.aspectSetupComboBox.setSelectedIndex(4);
			threeToneGui.readLayoutSetup();
		}
		if (listenString.equals("orientSetup"))
			threeToneGui.readLayoutSetup();
		if (listenString.equals("flipSetup"))
			threeToneGui.readLayoutSetup();
		if (listenString.equals("Line Draw.Extension"))
			threeToneGui.lineDraw(0);
		if (listenString.equals("Line Draw.Unit"))
			threeToneGui.lineDraw(1);
		if (listenString.equals("Line Draw.Measure"))
			threeToneGui.lineDraw(2);
		if (listenString.equals("Line Draw.Origin"))
			threeToneGui.lineDraw(3);
		if (listenString.equals("Line Draw.Clear Extension Lines"))
			threeToneGui.clearExtensionLines();
		
		if (listenString.equals("Line Check Box"))
			threeToneGui.plotImage();
		if (listenString.equals("Settings.Show"))
		{
			threeToneGui.globalSettingsPane.setVisible(true);
			threeToneGui.resizeMainPanels();
			threeToneGui.plotImage();
		}
		if (listenString.equals("Settings.Hide"))
		{
			threeToneGui.globalSettingsPane.setVisible(false);
			threeToneGui.resizeMainPanels();
			threeToneGui.plotImage();
		}
		if (listenString.equals("Settings.Custom Color Palette"))
		{
			threeToneGui.customColorPalette();
			threeToneGui.resizeMainPanels();
			threeToneGui.plotImage();
		}
		if (listenString.equals("Color Match"))
		{
			threeToneGui.colorMatchingOn = true;
			threeToneGui.matchColors(true);
		}
		if (listenString.equals("Color Mix"))
		{
			threeToneGui.colorMatchingOn = false;
		}
	}
	public void stateChanged(ChangeEvent e) 
	{
//		System.out.println(listenString);
		if (listenString.equals("Sat Slider Changed"))
			threeToneGui.satSliderChanged();		
		if (listenString.equals("Color Slider Changed"))
			threeToneGui.colorSliderChanged();
		if (listenString.equals("imagePaneChanged"))
			threeToneGui.plotImage();
		if (listenString.equals("MainTabbedChanged"))
		{
			if (threeToneGui.mainTabbedPane.getSelectedIndex() != 0) return;
			threeToneGui.resizeMainPanels();
			threeToneGui.plotImage();
		}
		
		if (threeToneGui.ignoreLevelChange || threeToneGui.autoLevelReset)
		{
			return;
		}
		else
		{
			if (listenString.equals("Level 0 Slider Changed"))
				threeToneGui.levelSliderChanged(0);
			if (listenString.equals("Level 1 Slider Changed"))
				threeToneGui.levelSliderChanged(1);
			if (listenString.equals("Level 2 Slider Changed"))
				threeToneGui.levelSliderChanged(2);
			if (listenString.equals("Level 3 Slider Changed"))
				threeToneGui.levelSliderChanged(3);
		}
	}

	public void mouseClicked(MouseEvent e) 
	{
//		System.out.println(listenString);
		if (listenString.equals("CL Panel Mouse"))
			threeToneGui.mouseClickedOnColorImage(e);	
		if (listenString.equals("BW Panel Mouse"))
			threeToneGui.mouseClickedOnBWImage(e);	
		if (listenString.equals("Thumbnail Clicked"))
			threeToneGui.mainTabbedPane.setSelectedIndex(0);	
				
	}

	public void mouseEntered(MouseEvent e) 
	{
		
	}

	public void mouseExited(MouseEvent e) 
	{
		
	}

	public void mousePressed(MouseEvent e) 
	{
		
	}

	public void mouseReleased(MouseEvent e) 
	{
		
	}

	public void componentHidden(ComponentEvent e) 
	{
	}

	public void componentMoved(ComponentEvent e) 
	{
	}

	public void componentResized(ComponentEvent e) 
	{
//		System.out.println(listenString);
		if (threeToneGui.mainTabbedPane.getSelectedIndex() != 0) return;
		threeToneGui.resizeMainPanels();
		threeToneGui.plotImage();
	}

	public void componentShown(ComponentEvent e) 
	{
	}



}
